/**
 * @Title: MonitorController.java
 * @Package: com.itic.appbase.applications.monitor.controller
 * @author: idong
 * @date: May 22, 2015 3:14:30 PM
 * @version: V1.0
 */

package com.itic.appbase.applications.monitor.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.monitor.service.JVMMonitorService;
import com.itic.appbase.applications.monitor.service.ServerMonitorService;
import com.itic.appbase.applications.monitor.vo.CPUInfo;
import com.itic.appbase.applications.monitor.vo.FileSystemInfo;
import com.itic.appbase.applications.monitor.vo.JVMInfo;
import com.itic.appbase.applications.monitor.vo.JavaEnvInfo;
import com.itic.appbase.applications.monitor.vo.MemInfo;
import com.itic.appbase.applications.monitor.vo.ServerInfo;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;

/**
 * <p>
 * Company: itic
 * </p>
 * 
 * @author: idong
 * @date: May 22, 2015 3:14:30 PM
 * @version: V1.0
 */

@Controller
@RequestMapping(value = "/monitor")
public class MonitorController
        extends BaseController {
    @Autowired
    ServerMonitorService serverMonitorService;
    @Autowired
    JVMMonitorService jVMMonitorService;

    /**
     * 初始化页面
     * 
     * @return
     */
    @RequestMapping(value = "/")
    public String init() {
        return "/monitor/init";
    }

    /**
     * 服務器頁面初始化
     * 
     * @return
     */
    @RequestMapping(value = "/server")
    public String initServer() {
        return "/monitor/server";
    }

    /**
     * 服务器信息
     * 
     * @return
     */
    @RequestMapping(value = "/serverInfo")
    @ResponseBody
    public Map<String, Object> displayServerStatus() {
        Map<String, Object> result = new HashMap<String, Object>();
        // 获取信息
        ServerInfo server = serverMonitorService.getServerInfo();
        // 返回信息
        result.put("server", server);
        return result;
    }

    /**
     * 文件系统信息
     * 
     * @return
     */
    @RequestMapping(value = "/diskInfo")
    @ResponseBody
    public Map<String, Object> displayFileSystemStatus() {
        Map<String, Object> result = new HashMap<String, Object>();
        // 获取信息
        List<FileSystemInfo> fs = serverMonitorService.getFileSystemInfo();
        // 返回信息
        result.put(IticConst.DATA, fs);
        result.put(IticConst.TOTAL_RECORDS, fs.size()); // 总记录
        result.put(IticConst.DISPLAY_RECORDS, fs.size());
        result.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        return result;
    }

    /**
     * CPU信息
     * 
     * @return
     */
    @RequestMapping(value = "/cpuInfo")
    @ResponseBody
    public Map<String, Object> displayCPUStatus() {
        Map<String, Object> result = new HashMap<String, Object>();
        // 获取信息
        CPUInfo cpu = serverMonitorService.getCpuInfo();
        // 返回信息
        result.put("cpu", cpu);
        return result;
    }

    /**
     * 内存信息
     * 
     * @return
     */
    @RequestMapping(value = "/memoryInfo")
    @ResponseBody
    public Map<String, Object> displayMemoryStatus() {
        Map<String, Object> result = new HashMap<String, Object>();
        // 获取信息
        MemInfo mem = serverMonitorService.getMemInfo();
        // 返回信息
        result.put("mem", mem);
        return result;
    }

    /**
     * JVM頁面初始化
     * 
     * @return
     */
    @RequestMapping(value = "/jvm")
    public String initJvm() {
        return "/monitor/jvm";
    }

    /**
     * Java环境信息
     * 
     * @return
     */
    @RequestMapping(value = "/appInfo")
    @ResponseBody
    public Map<String, Object> displayJavaStatus() {
        Map<String, Object> result = new HashMap<String, Object>();
        // 获取信息
        JavaEnvInfo jei = jVMMonitorService.getJavaEnvInfo();
        // 返回信息
        result.put("javaEnv", jei);
        return result;
    }

    /**
     * JVM内存信息
     * 
     * @return
     */
    @RequestMapping(value = "/jvmInfo")
    @ResponseBody
    public Map<String, Object> displayJVMMemStatus() {
        Map<String, Object> result = new HashMap<String, Object>();
        // 获取信息
        JVMInfo ji = jVMMonitorService.getJvmInfo();
        // 返回信息
        result.put("jvm", ji);
        return result;
    }

    @RequestMapping(value = "/dbs")
    public String initDataSource() {
        return "/monitor/datasource";
    }

    @RequestMapping(value = "/connectionInfo")
    public String initDataSourceConn(String dataSourceId, Model model) {
        model.addAttribute("dataSourceId", dataSourceId);
        return "/monitor/connectionInfo";
    }

    @RequestMapping(value = "/sql")
    public String initSQL(String dataSourceId, Model model) {
        model.addAttribute("dataSourceId", dataSourceId);
        return "/monitor/sql";
    }

    @RequestMapping(value = "/sql-detail")
    public String initSQLDetail(String sqlId, Model model) {
        model.addAttribute("sqlId", sqlId);
        return "/monitor/sql-detail";
    }

    @RequestMapping(value = "/uri")
    public String initURI() {
        return "/monitor/weburi";
    }

    @RequestMapping(value = "/uri-detail")
    public String initURIDetail(String uri, Model model) {
        model.addAttribute("uri", uri);
        return "/monitor/weburi-detail";
    }

    @RequestMapping(value = "/session")
    public String initSession() {
        return "/monitor/websession";
    }

    @RequestMapping(value = "/session-detail")
    public String initSessionDetail(String sessionId, Model model) {
        model.addAttribute("sessionId", sessionId);
        return "/monitor/websession-detail";
    }
}
